;; Benchmarks of the Ramer-Douglas-Peucker functions
;; Copyright (C) 2023  Rocks Mazama
;; 
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; 
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;; 
;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

(ns bench
  (:require
   [criterium.core :as criterium]
   [mazama.compgeom.ramer-douglas-peucker :refer [classic-rdp heap-rdp]]))

(defn- prng-bound [seed bound]
  (let [frand (java.util.Random. seed)]
    (fn [] (.nextInt frand bound))))

(defn- prng-min-max
  "Return a pseudo-random number generator using seed.
  Each call to the prng returns min <= value <= max."
  [seed min max]
  (let [bound (inc (- max min))
        rndm (prng-bound seed bound)]
    (fn [] (+ min (rndm)))))

(defn- make-random-data [seed n lo hi]
  (let [prng (prng-min-max seed lo hi)]
    (repeatedly n #(vector (prng) (prng)))))

(defn- run [msg rdp points epsilon]
  (println (str \newline msg) "epsilon" epsilon)
  (criterium/quick-bench (rdp points epsilon)))

(defn -main []
  (let [points (doall (make-random-data 37 1234 -99999 99999))
        epsilons [18 235 4567]]
    (doseq [epsilon epsilons] (run "classic-rdp small" classic-rdp points epsilon))
    (doseq [epsilon epsilons] (run "heap-rdp small" heap-rdp points epsilon)))
  (let [points (doall (make-random-data -37 123456 -999999 999999))
        epsilons [18 2345 67890]]
    (doseq [epsilon epsilons] (run "classic-rdp big" classic-rdp points epsilon))
    (doseq [epsilon epsilons] (run "heap-rdp big" heap-rdp points epsilon)))
  (let [points (doall (make-random-data -357 1234567 -9999999 999999))
        epsilons [18 23456 789012]]
    (doseq [epsilon epsilons] (run "classic-rdp bigger" classic-rdp points epsilon))
    (doseq [epsilon epsilons] (run "heap-rdp bigger" heap-rdp points epsilon))))
